package com.yw.swagger.controller;

import com.yw.swagger.customNote.*;
import com.yw.swagger.model.SpNotice;
import com.yw.swagger.model.SpNoticeFile;
import com.yw.swagger.service.SpNoticeService;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.Convert;
import com.yw.swagger.tool.Utils.StringUtils;
import com.yw.swagger.tool.controller.BaseController;
import com.yw.swagger.tool.page.TableDataInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@Api(tags="通知接口")
@RequestMapping("/sys_notice")
public class SpNoticeController extends BaseController {

    @Autowired
    private SpNoticeService spNoticeService;


    @qw
    @ApiOperation(value = "分页所有查询通知",notes = "分页所有查询通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="数据条数",required = true)
    })
    @PostMapping("/all_list")
    @ResponseBody
    public AjaxResult allList(Integer pageNum, Integer pageSize) {
        startPage();
        List<SpNotice> l = spNoticeService.selectNoticesAllList();
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @qw
    @ApiOperation(value = "分页查询收信人全部通知",notes = "分页查询收信人全部通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="toUser",value="收信人账号",required = true),
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="数据条数",required = true)
    })
    @PostMapping("/to_list")
    @ResponseBody
    public AjaxResult toList(String toUser,Integer pageNum, Integer pageSize) {
        SpNotice sn = new SpNotice();
        sn.setToUser(toUser);
        startPage();
        List<SpNotice> l = spNoticeService.selectNoticesList(sn);
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @qw
    @ApiOperation(value = "分页查询发信人全部通知",notes = "分页查询发信人全部通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="fromUser",value="发信人账号",required = true),
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="数据条数",required = true)
    })
    @PostMapping("/from_list")
    @ResponseBody
    public AjaxResult fromList(String fromUser,Integer pageNum, Integer pageSize) {
        SpNotice sn = new SpNotice();
        sn.setFromUser(fromUser);
        startPage();
        List<SpNotice> l = spNoticeService.selectNoticesList(sn);
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @qw
    @ApiOperation(value = "分页查询收信人全部未读通知",notes = "分页查询收信人全部未读通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="toUser",value="收信人账号",required = true),
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="数据条数",required = true)
    })
    @PostMapping("/to_answer_list")
    @ResponseBody
    public AjaxResult toAnswerList(String toUser,Integer pageNum, Integer pageSize) {
        SpNotice sn = new SpNotice();
        sn.setToUser(toUser);
        sn.setAnswerFlag("0");
        startPage();
        List<SpNotice> l = spNoticeService.selectNoticesAnswerList(sn);
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }

    @qw
    @ApiOperation(value = "分页查询收信人全部已读通知",notes = "分页查询收信人全部已读通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="toUser",value="收信人账号",required = true),
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="数据条数",required = true)
    })
    @PostMapping("/to_no_answer_list")
    @ResponseBody
    public AjaxResult toNoAnswerList(String toUser,Integer pageNum, Integer pageSize) {
        SpNotice sn = new SpNotice();
        sn.setAnswerFlag("1");
        sn.setToUser(toUser);
        startPage();
        List<SpNotice> l = spNoticeService.selectNoticesAnswerList(sn);
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        ar.put("rows",td.getRows());
        ar.put("total",td.getTotal());
        return ar;
    }


    @qw
    @ApiOperation(value = "收信人查询通知详情信息",notes = "收信人查询通知详情信息")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="NoticeID",value="通知uuid",required = true),
            @ApiImplicitParam(name="toUser",value="收信人账号",required = true)
    })
    @PostMapping("/notice_info")
    @ResponseBody
    public AjaxResult noticeInfo(String NoticeID,String toUser) {
        SpNotice sn = spNoticeService.selectNoticeInfoByNoticeId(NoticeID,toUser);
        if(StringUtils.isNull(sn)){
            return error("没有查询到该条通知");
        }
        if(sn.getAnswerFlag().equals("0")){
            spNoticeService.setAnswer(sn);
        }

        List<SpNoticeFile> snf = spNoticeService.selectNoticeFileByNoticeId(NoticeID);
        Map m = new HashMap();
        m.put("sn",sn);
        m.put("snf",snf);
        return AjaxResult.success(m);
    }

    @qw
    @ApiOperation(value = "发信人查询通知详情信息",notes = "发信人查询通知详情信息")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="NoticeID",value="通知uuid",required = true),
    })
    @PostMapping("/notice_from_info")
    @ResponseBody
    public AjaxResult noticeFromInfo(String NoticeID) {
        List<SpNotice> sn = spNoticeService.selectNoticeFromInfoByNoticeId(NoticeID);
        if(StringUtils.isNull(sn)){
            return error("没有查询到该条通知");
        }
        List<SpNoticeFile> snf = spNoticeService.selectNoticeFileByNoticeId(NoticeID);
        Map m = new HashMap();
        m.put("sn",sn);
        m.put("snf",snf);
        return AjaxResult.success(m);
    }



    @qw
    @ApiOperation(value = "新增通知",notes = "新增通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name = "toUsers",value = "收信人账号列表 示例: user1,user2,user3"),
            @ApiImplicitParam(name = "lxrs",value = "联系人列表 示例: lxrs1,lxrs2,lxrs3"),
            @ApiImplicitParam(name = "lxdhs",value = "联系人电话列表 示例: lxrdhs1,lxrdhs2,lxrdhs3"),
            @ApiImplicitParam(name = "fileNames",value = "附件名称列表 示例: lxrdhs1,lxrdhs2,lxrdhs3"),
            @ApiImplicitParam(name = "filePath",value = "附件路径列表 示例: lxrdhs1,lxrdhs2,lxrdhs3")
    })
    @ApiOperationSupport(ignoreParameters  = {"toUser","lxr","lxdh"})
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(SpNotice spNotice,String toUsers,String lxrs,String lxdhs,String fileNames,String filePath) {
        if(StringUtils.isNull(spNotice) || StringUtils.isNull(toUsers)){
            return error("不能存在空参数");
        }

        return AjaxResult.success(spNoticeService.addNotice(spNotice,toUsers,lxrs,lxdhs,fileNames,filePath));
    }

    @qw
    @ApiOperation(value = "收信人删除通知",notes = "收信人删除通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="noticeId",value="uuid"),
            @ApiImplicitParam(name = "toUser", value = "收信人账号")
    })
    @PostMapping("/delete_to")
    @ResponseBody
    public AjaxResult deleteTo(String noticeId,String toUser) {
        if(StringUtils.isNull(noticeId) || StringUtils.isNull(toUser)){
            return error("不能存在空参数");
        }
        return AjaxResult.success(spNoticeService.delNoticeTo(noticeId,toUser));
    }

    @qw
    @ApiOperation(value = "收信人多条删除通知",notes = "收信人多条删除通知")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 500,message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name="author",value="权限码",required = true),
            @ApiImplicitParam(name="noticeIds",value="示例: uuid1,uuid2,uuid3"),
            @ApiImplicitParam(name="toUser",value="收信人账号")
    })
    @PostMapping("/dels_to")
    @ResponseBody
    public AjaxResult dels(String noticeIds,String toUser) {
        if(StringUtils.isNull(noticeIds) || StringUtils.isNull(toUser)){
            return error("不能存在空参数");
        }

        return AjaxResult.success(spNoticeService.deleteByIds(noticeIds,toUser));
    }
}
